package com.shqcjd.preinstallmobile.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.orhanobut.logger.Logger;
import com.shqcjd.preinstallmobile.R;
import com.shqcjd.preinstallmobile.app.MyApp;
import com.shqcjd.preinstallmobile.ui.activity.remote.PullVideoCallActivity;
import com.shqcjd.preinstallmobile.ui.base.BaseActivity;
import com.shqcjd.preinstallmobile.ui.base.BasePresenter;
import com.shqcjd.preinstallmobile.util.UIUtils;
import com.shqcjd.preinstallmobile.widget.BlurTransformation;
import com.shqcjd.preinstallmobile.widget.WaveView;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import io.agora.AgoraAPI;
import io.agora.AgoraAPIOnlySignal;

/**
 * 答复接听页
 */
public class AnswerActivity extends BaseActivity {

    @BindView(R.id.iv_bg_img)
    ImageView ivBgImg;
    @BindView(R.id.wv_icon_bg)
    WaveView wvIconBg;
    @BindView(R.id.civ_icon)
    CircleImageView civIcon;
    @BindView(R.id.fl_close_btn)
    FrameLayout flCloseBtn;
    @BindView(R.id.fl_answer_btn)
    FrameLayout flAnswerBtn;
    private static Activity activity;
    private AgoraAPIOnlySignal mAgoraAPI;
    private String channelID;
    private String callerId;
    private String info;

    public static Activity getInstance(){
        return activity;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_answer;
    }

    @Override
    public void initView() {
        activity = this;
        initIcon();
        channelID = getIntent().getStringExtra("channelID");
        callerId = getIntent().getStringExtra("callerId");
        info = getIntent().getStringExtra("info");

        mAgoraAPI = MyApp.the().getmAgoraAPI();
    }

    private void initIcon() {
        // 高斯模糊
        Drawable drawable = ActivityCompat.getDrawable(this, R.mipmap.touxiang1);
        Glide.with(this)
                .load(drawable)
                .apply(RequestOptions.bitmapTransform(new BlurTransformation(this,20, 6)))
                .into(ivBgImg);

        //水波纹效果
        wvIconBg.setDuration(5000);
        wvIconBg.setStyle(Paint.Style.FILL);
        wvIconBg.setSpeed(1000);
        wvIconBg.setColor(ContextCompat.getColor(this,R.color.white));
        wvIconBg.setInterpolator(new LinearOutSlowInInterpolator());
        wvIconBg.setMaxRadius(UIUtils.dp2px(123));
        wvIconBg.start();
    }


    @OnClick({R.id.fl_close_btn, R.id.fl_answer_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fl_close_btn://拒绝
                mAgoraAPI.channelInviteRefuse(channelID,callerId,0,null);
                finish();
                break;
            case R.id.fl_answer_btn://同意
                mAgoraAPI.channelInviteAccept(channelID,callerId,0,null);
                Intent intent = new Intent(AnswerActivity.this, PullVideoCallActivity.class);
                Logger.i("Agora channelID:"+channelID+" 呼叫方:"+callerId);
                intent.putExtra("channelID", channelID);
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        addCallback();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void addCallback() {
        mAgoraAPI = MyApp.the().getmAgoraAPI();
        mAgoraAPI.callbackSet(new AgoraAPI.CallBack() {
            @Override
            public void onError(String s, int i, String s1) {
                Logger.e("Agora 信令onError s:" + s + " i:" + i + " s1:" + s1);
            }

            //呼叫取消时的回调
            @Override
            public void onInviteEndByPeer(String channelID, String account, int uid, String extra) {
                super.onInviteEndByPeer(channelID, account, uid, extra);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Logger.i("Agora 呼叫取消:");
                        finish();
                    }
                });
            }

            //收到对方呼叫时调用
            @Override
            public void onInviteReceived(String channelID, String account, int uid, String extra) {
                super.onInviteReceived(channelID, account, uid, extra);
                Logger.i("AgoraService：收到呼叫 房间:" + channelID + " 账号:" + account + " 备注:" + extra);
                Intent intent = new Intent(MyApp.getmContext(), AnswerActivity.class);
                intent.putExtra("channelID", channelID);
                intent.putExtra("callerId", account);//主叫方id
                intent.putExtra("info", extra);//主叫方id
                startActivity(intent);

            }
        });
    }
}
